﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace SLSearchNavSync.HelperClasses
{
    /// <summary>
    /// The Class containing all methods related to error logging in the solution
    /// </summary>
    public static class UlsHelper
    {
        /// <summary>
        /// Method for writing errors to the SharePoint ULS
        /// </summary>
        /// <param name="strMessage">The error message</param>.
        /// <param name="tsTraceSeverity">The severity of the trace</param>
        /// <param name="esEventSeverity">The Event Severity</param>
        /// <param name="strCategoryName">The category of the trace</param>
        public static void WriteUlsEntry(string strMessage, TraceSeverity tsTraceSeverity, EventSeverity esEventSeverity, string strCategoryName)
        {
            WriteUlsEntry(null, strMessage, tsTraceSeverity, esEventSeverity, strCategoryName, 101);
        }

        /// <summary>
        /// Method for writing errors to the SharePoint ULS
        /// </summary>
        /// <param name="ex">The Captured Exception</param>
        /// <param name="tsTraceSeverity">The severity of the trace</param>
        /// <param name="esEventSeverity">The Event Severity</param>
        /// <param name="strCategoryName">The category of the trace</param>
        public static void WriteUlsEntry(Exception ex, TraceSeverity tsTraceSeverity, EventSeverity esEventSeverity, string strCategoryName)
        {
            WriteUlsEntry(ex, "", tsTraceSeverity, esEventSeverity, strCategoryName, 101);
        }

        /// <summary>
        /// Method for writing errors to the SharePoint ULS
        /// </summary>
        /// <param name="ex">The Captured Exception</param>
        /// <param name="tsTraceSeverity">The severity of the trace</param>
        /// <param name="strMessage">The error message</param>
        /// <param name="strCategoryName">The category of the trace</param>
        public static void WriteUlsEntry(Exception ex, TraceSeverity tsTraceSeverity, string strMessage, string strCategoryName)
        {
            WriteUlsEntry(ex, strMessage, tsTraceSeverity, EventSeverity.Error, strCategoryName, 101);
        }

        /// <summary>
        /// Method for writing errors to the SharePoint ULS
        /// </summary>
        /// <param name="ex">The Captured Exception</param>
        /// <param name="tsTraceSeverity">The severity of the trace</param>
        /// <param name="esEventSeverity">The Event Severity</param>
        /// <param name="strMessage">The overloaded error message.  Pass an empty string to log the exception message.</param>.
        /// <param name="strCategoryName">The category of the trace</param>
        /// <param name="uintCategoryId">The application-defined identifier for the trace</param>
        public static void WriteUlsEntry(Exception ex, string strMessage, TraceSeverity tsTraceSeverity, EventSeverity esEventSeverity, string strCategoryName, uint uintCategoryId)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate
            {
                var category = new SPDiagnosticsCategory(strCategoryName, tsTraceSeverity, esEventSeverity);

                if (string.IsNullOrEmpty(strMessage) && ex != null)
                {
                    SPDiagnosticsService.Local.WriteTrace(uintCategoryId, category, tsTraceSeverity,
                                                          string.Concat(ex.ToString(), ex.StackTrace), null);
                }
                else if (!string.IsNullOrEmpty(strMessage))
                {
                    if (ex == null)
                    {
                        SPDiagnosticsService.Local.WriteTrace(uintCategoryId, category, tsTraceSeverity, strMessage,
                                                              null);
                    }
                    else
                    {
                        SPDiagnosticsService.Local.WriteTrace(uintCategoryId, category, tsTraceSeverity,
                                                              strMessage + "\n\n" +
                                                              string.Concat(ex.ToString(), ex.StackTrace), null);
                    }
                }
            });
        }
    }
}
